package com.itsv.cyzx.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.itsv.cyzx.bean.TWBean;
import com.itsv.cyzx.db.DingCanDB;
import com.itsv.cyzx.util.LoadImageAysnc2;
import com.itsv.cyzx.util.LoadImageAysnc2.ImageCallBack;
import com.itsv.xhyy.R;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class TWNineAdapter extends BaseAdapter {
	private Context con = null;
	private ArrayList<TWBean> jlist = null;
	private LayoutInflater inflater;
	private LoadImageAysnc2 loadImageAysnc;
	// Ҫ������lisvView
	private GridView gridView;

	public TWNineAdapter(Context con, ArrayList<TWBean> jlist, GridView gridView) {
		this.con = con;
		this.jlist = jlist;
		this.gridView = gridView;
		this.inflater = LayoutInflater.from(this.con);
		loadImageAysnc = new LoadImageAysnc2(con);

	}

	public void setData(ArrayList<TWBean> jlist) {
		this.jlist = jlist;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return jlist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_twnine, null);
			holder = new ViewHolder();
			holder.titleView = (TextView) convertView
					.findViewById(R.id.twNine_name);
			holder.jiageView = (TextView) convertView
					.findViewById(R.id.twNine_jiage);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.twNine_image);
			holder.button = (ImageView) convertView
					.findViewById(R.id.twNine_dingcan);
			convertView.setTag(holder);
			convertView.setPadding(10, 10, 10, 10); // ÿ��ļ��
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final TWBean mm = jlist.get(position);
		if (mm != null) {
			holder.titleView.setText(mm.getTitle());
			holder.jiageView.setText(mm.getJiage());
			String images = mm.getTitle_pic();
			if (!images.equals("") && images != null) {
				holder.imageView.setTag(mm.getId());
				// ��ʼ�첽�������
				Drawable drawable = loadImageAysnc.loadImage(mm.getId(),
						images, mm.getId() + ".jpg", new ImageCallBack() {

							@Override
							public void imageLoaded(Drawable drawable,
									String iconSrc) {
								ImageView imageViewTag = (ImageView) gridView
										.findViewWithTag(mm.getId());
								if (imageViewTag != null) {
									imageViewTag
											.setBackgroundDrawable(drawable);
								}

							}
						});

				if (drawable == null) {
					holder.imageView.setBackgroundResource(R.drawable.cach);
				} else {
					holder.imageView.setBackgroundDrawable(drawable);
				}
			} else {
				holder.imageView.setBackgroundResource(R.drawable.cach);
			}
		}
		return convertView;
	}

	class ViewHolder {
		private TextView titleView;
		private TextView jiageView;
		private ImageView imageView;
		private ImageView button;
	}
}
